﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.Model;
using IHISCETeim.Library;
using IHISCETeim.BLL;


namespace IHISCETeim.Web.MachineSys.JSDWF
{
    public partial class JChuJiaGongWuListUpdateCurrFlow : PageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.查看 };
            }
        }
        protected override string DWZRef
        {
            get
            {
                return "JChuJiaGongWuListEasyUI";
            }
        }

        private List<JChuJiaTempFlowInfo> _listFlowTemp = null;
        protected List<JChuJiaTempFlowInfo> listFlowTemp
        {
            get
            {
                if (_listFlowTemp == null)
                    _listFlowTemp = JChuJiaGongWuListController.GetAllJChuJiaTempFlow();
                return _listFlowTemp;
            }
        }

        private List<JChuJiaItemDetailInfo> _listDetail = null;
        protected List<JChuJiaItemDetailInfo> listDetail
        {
            get
            {
                if (_listDetail == null)
                    _listDetail = JChuJiaGongWuListController.GetAllJChuJiaItemDetailByChuJGWId(ChuJGWId);
                return _listDetail;
            }
        }

        private List<JChuJiaSerialDetailInfo> _listSerial = null;
        protected List<JChuJiaSerialDetailInfo> listSerial
        {
            get
            {
                if (_listSerial == null)
                    _listSerial = JChuJiaGongWuListController.GetAllJChuJiaSerialDetailByChuJGWId(ChuJGWId);
                return _listSerial;
            }
        }

        private List<JChuJiaFlowDetailInfo> _listFlow = null;
        protected List<JChuJiaFlowDetailInfo> listFlow
        {
            get
            {
                if (_listFlow == null)
                    _listFlow = JChuJiaGongWuListController.GetAllJChuJiaFlowDetail(ChuJGWId);
                return _listFlow;
            }
        }

        protected void InitBind()
        {
            //int rowSUM = listDetail.Count();
            ////管制物料
            //if (rowSUM < 10)
            //{

            //    for (int i = 0; i < 10 - rowSUM; i++)
            //    {
            //        listDetail.Add(new JChuJiaItemDetailInfo());
            //    }
            //}

            int rowSUM = listSerial.Count();
            if (rowSUM < 10)
            {
                for (int i = 0; i < 10 - rowSUM; i++)
                {
                    listSerial.Add(new JChuJiaSerialDetailInfo());
                }
            }

            this.rplistDetail.DataSource = listDetail;
            this.rplistDetail.DataBind();

            this.rplistSerial.DataSource = listSerial;
            this.rplistSerial.DataBind();

            rpFlowTemp.DataSource = listFlow;
            rpFlowTemp.DataBind();
        }

        private JChuJiaGongWuListInfo _obj;
        protected JChuJiaGongWuListInfo obj
        {
            get
            {
                if (_obj == null)
                    _obj = JChuJiaGongWuListController.GetListByID(ChuJGWId);
                return _obj;
            }
        }
        protected int ChuJGWId
        {
            get
            {
                return GetRequestValue<int>("ChuJGWId", 0);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetRequestValue<string>("ajaxmethod", "") == "add")
            {
                addmenu();
            }
            //if (GetRequestValue<string>("ajaxsingupdate", "") == "updateflow")
            //{
            //    updateflow();
            //}
            
            InitBind();
        }

        private void updateflow()
        {
            //JChuJiaFlowDetailInfo obj333=Request.SerializableObject<JChuJiaFlowDetailInfo>();
            //bool b=   JChuJiaGongWuListController.UpdateJChuJiaFlowDetail(obj333);
            //if(b)
            //    SuccessJson("成功",DWZRef);
            //else
            //    ErrorJson("失败");
        }

        private void addmenu()
        {
            JChuJiaGongWuListInfo obj2 = Request.SerializableObject<JChuJiaGongWuListInfo>();
            obj2.ScanDate = obj.ScanDate;
            obj2.ScanFilePath = obj.ScanFilePath;
            char[] caSplit = new char[] { ',' };
            ////管制零件
            //string[] ItemCodes = GetRequestValue<string>("ItemCode", "").Split(caSplit);
            //string[] ItemNames = GetRequestValue<string>("ItemName", "").Split(caSplit);
            //string[] Quantitys = GetRequestValue<string>("Quantity", "").Split(caSplit);
            //string[] Notes = GetRequestValue<string>("Note", "").Split(caSplit);

            //管制机号
            string[] JHSerialNumbers = GetRequestValue<string>("JHSerialNumber", "").Split(caSplit);
            string[] SJSerialNumbers = GetRequestValue<string>("SJSerialNumber", "").Split(caSplit);
            string[] SerialNotes = GetRequestValue<string>("SerialNote", "").Split(caSplit);

            //管制零件
            //List<JChuJiaItemDetailInfo> _listItem = new List<JChuJiaItemDetailInfo>();
            //for (int rowIndex = 0; rowIndex < ItemCodes.Count(); rowIndex++)
            //{
            //    JChuJiaItemDetailInfo _itmItem = new JChuJiaItemDetailInfo();


            //    _itmItem.ItemCode = ItemCodes[rowIndex];
            //    if (string.IsNullOrEmpty(_itmItem.ItemCode))
            //        continue;

            //    _itmItem.ItemName = ItemNames[rowIndex];
            //    _itmItem.Quantity = Convert.ToInt32(Quantitys[rowIndex]);
            //    _itmItem.Note = Notes[rowIndex];
            //    _listItem.Add(_itmItem);
            //}

            //管制机号
            List<JChuJiaSerialDetailInfo> _listSerial = new List<JChuJiaSerialDetailInfo>();
            for (int rowIndex = 0; rowIndex < JHSerialNumbers.Count(); rowIndex++)
            {
                JChuJiaSerialDetailInfo _itmItem = new JChuJiaSerialDetailInfo();
                _itmItem.JHSerialNumber = JHSerialNumbers[rowIndex];
                _itmItem.SJSerialNumber = SJSerialNumbers[rowIndex];
                _itmItem.Note = SerialNotes[rowIndex];
                if (string.IsNullOrEmpty(_itmItem.JHSerialNumber) && string.IsNullOrEmpty(_itmItem.SJSerialNumber) && string.IsNullOrEmpty(_itmItem.Note))
                    continue;
                _listSerial.Add(_itmItem);
            }
            JChuJiaFlowDetailInfo objFlowDetail = Request.SerializableObject<JChuJiaFlowDetailInfo>();
            //objFlowDetail.IsSend = true;
            bool b = JChuJiaGongWuListController.UpdateJChuJiaFlowDetail(objFlowDetail, _listSerial);
            if (b)
                SuccessJson("成功", DWZRef);
            else
                ErrorJson("失败");

            //if (r)
            //    SuccessJson("已成功修改初加工物管制票信息", this.DWZRef);
            //else
            //    ErrorJson("修改失败");

        }
    }
}